package com.qumitech.visualnovel.controller.rest.novel;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.dto.BookQueryDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.service.novel.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.qumitech.visualnovel.model.dto.ResourceUpdateDTO;
import com.qumitech.visualnovel.model.dto.ResourceQueryDTO;
import com.qumitech.visualnovel.model.dto.ResourceRelationOptionsDTO;

@RestController
@Api(value = "资源管理模块", description = "资源管理相关api")
public class ResourceRest {
    @Resource
    private ResourceService resourceService;

    @ApiOperation(value = "上传文件", notes = "上传文件")
    @ApiImplicitParams({
        @ApiImplicitParam(name="mainType", value="主类型: picture|audio", required=true),
        @ApiImplicitParam(name="subType", value="子类型: picture: body|hair|outfit|object|bgp audio: bgm|sound", required=true),
        @ApiImplicitParam(name="decorationType", value="层级名称", required=false),
        @ApiImplicitParam(name="sexIfPicture", value="图片人物的性别", required=false),
        @ApiImplicitParam(name="fileList", value="文件列表", required=true),
        @ApiImplicitParam(name="relationId", value="关联资源id", required=false),
        @ApiImplicitParam(name="name", value="资源名称", required=false),
        @ApiImplicitParam(name="isBatch", value="批量上传标志: true|false", required=false)
    })
    @PostMapping("/api/resource/upload")
    public BaseResponse upload(HttpServletRequest request) {
        return resourceService.upload(request);
    }

    @GetMapping(value = "/api/resource/download")
    public void download(HttpServletResponse response, @RequestParam(value="filePath") String filePath) {
        resourceService.download(response, filePath);
    }

    @ApiOperation(value = "删除资源文件")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "资源id", required = true)
    })
    @DeleteMapping("/api/resource/{id}")
    public BaseResponse delete(@PathVariable("id") Long id) {
        return resourceService.delete(id);
    }

    @ApiOperation(value = "修改资源文件", notes = "修改资源文件")
    @PostMapping("/api/resource/{id}")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id", value="资源id", required=true),
        @ApiImplicitParam(name="fileList", value="资源文件", required=true),
        @ApiImplicitParam(name="name", value="资源名称", required=false)
    })
    public BaseResponse update(@PathVariable("id") Long id, HttpServletRequest request) {
        return resourceService.update(id, request);
    }

    @ApiOperation(value = "修改资源属性", notes = "修改资源属性")
    @PatchMapping("/api/resource/{id}")
    public BaseResponse updateAttrbute(@PathVariable("id") Long id, @RequestBody ResourceUpdateDTO param) {
        return resourceService.updateAttrbute(id, param);
    }

    @ApiOperation(value = "查询资源列表", notes = "查询资源列表")
    @GetMapping("/api/resource/list")
    public BaseResponse list(ResourceQueryDTO param) {
        return resourceService.list(param);
    }

    @ApiOperation(value = "查询资源选项", notes = "查询资源选项")
    @GetMapping("/api/resource/relation/options")
    public BaseResponse relationOptions(ResourceRelationOptionsDTO param) {
        return resourceService.relationOptions(param);
    }

    @ApiOperation(value = "查询关联资源列表", notes = "查询关联资源列表")
    @GetMapping("/api/resource/relation/list")
    public BaseResponse relationList(@RequestParam(value="id", required=true) Long id) {
        return resourceService.relationList(id);
    }

    @ApiOperation(value = "查询小说列表", notes = "查询小说列表")
    @GetMapping("/api/resource/book/list")
    public BaseResponse list(BookQueryDTO param) {
        return resourceService.bookList(param);
    }
}
